﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using VehicleManagement.Models;
using VehicleManagement.Codes;

namespace VehicleManagement.ControllersServices
{
    public class DriverServices
    {

        public IList<DriverInfo> GetAllDriver()
        {
            IList<DriverInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<DriverInfo>("SELECT_DRIVERINFO", null);
            return lstModel;

        }
        /// <summary>
        /// 根据ID查找司机
        /// </summary>
        /// <param name="id">司机ID</param>
        /// <returns>DriverInfo</returns>
        public DriverInfo GetDriverByDriverID(int id)
        {
            IList<DriverInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<DriverInfo>("SELECT_DRIVERINFO_BY_ID", id);
            DriverInfo driverInfo = lstModel.Where(u => u.DriverID == id).First();
            return driverInfo;

        }
        /// <summary>
        /// 插入新司机
        /// </summary>
        /// <param name="driver"></param>
        /// <returns></returns>
        public object CreateDriver(DriverInfo driver)
        {
            return SqlMapperManager.Instance.Get().Insert("INSERT_USER", driver);
        }

        /// <summary>
        /// 修改司机信息
        /// </summary>
        /// <param name="driver"></param>
        /// <returns></returns>
        public int EditDriver(DriverInfo driver)
        {
            int row = SqlMapperManager.Instance.Get().Update("UPDATE_DRIVERINFO", driver);
            return row;
        }
        /// <summary>
        /// 删除司机
        /// </summary>
        /// <param name="id">司机的ID</param>
        /// <returns></returns>
        public int DeleteDriver(int id)
        {
            int i = SqlMapperManager.Instance.Get().Delete("DELETE_DRIVERINFO", id);
            return i;
        }


        public string GetDriverNameByDriverID(int driverid)
        {
            string driverNumber="";
            return driverNumber = SqlMapperManager.Instance.Get().QueryForObject("SELECT_DRIVERNAME_BY_ID", driverid).ToString();

        }


        public int  GetDriverIdByDriverName(string driverName)
        {
            int driverID = 0;
            return driverID = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_DRIVERID_BY_NAME", driverName));

        }

        public IList<string> GetAllDriverNameByFree()
        {
            IList<string> listDriverName = SqlMapperManager.Instance.Get().QueryForList<string>("SELECT_DriverName_BY_Free", 1);
            return listDriverName;
        }


        public IList<DriverInfo> SearchAllDriver(Hashtable ht)
        {
            IList<DriverInfo> lstDriver = new List<DriverInfo> { };
            lstDriver = SqlMapperManager.Instance.Get().QueryForList<DriverInfo>("SEARCH_DRIVER", ht);
            return lstDriver;
        }




        /// <summary>
        /// 构造Hastable
        /// </summary>
        /// <param name="ht"></param>
        /// <param name="skip"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public IList<DriverInfo> SearchAllDriver(Hashtable ht, int skip, int max)
        {
            IList<DriverInfo> lstDriver = SqlMapperManager.Instance.Get().QueryForList<DriverInfo>("SEARCH_DRIVER", ht,skip,max);
            return lstDriver;
        }

        public int CountAllDrivers(Hashtable ht)
        {
            return SearchAllDriver(ht).Count;
        }


	public bool DriverLogin(string LoginName, string password)
        {
            string realPassword = (string)SqlMapperManager.Instance.Get().QueryForObject("SELECT_PASSWORD_BY_LOGIN_NAME", LoginName);
            return (realPassword == password);
        }

        public int GetDriverIdByLoginName(string loginName)
        {
            int driverID = 0;
            return driverID = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_DRIVERID_BY_LOGIN_NAME", loginName));
        }

        /// <summary>
        /// 根据司机的ID，修改其目前的状态，返回1修改成功
        /// </summary>
        /// <returns>返回1时，修改成功，返回-1说明修改失败</returns>
        public int UpdateDriverState(int driverID,int state)
        {
           int Free_busy=-1;
            Hashtable hastable=new Hashtable();
            if (state >= 0)
            {
                if (state == 1)
                {
                    Free_busy = 1;
                }
                if (state == 0)
                {
                    Free_busy = 0;
                }
                hastable.Add("DriverState",Free_busy);
                hastable.Add("DriverID", driverID);
                int ok=SqlMapperManager.Instance.Get().Update("UPDATE_DRIVERSTATE", hastable);
                return ok;
            }
            else
            {
                return -1;
            }
            

            
           
 
        }


       
    }
}